package com.walkline.util.ui;

import com.walkline.app.WeatherAppConfig;
import com.walkline.weather.inf.Place;

import net.rim.device.api.io.File;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Characters;
import net.rim.device.api.system.Display;
import net.rim.device.api.system.KeypadListener;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.TouchEvent;
import net.rim.device.api.ui.component.LabelField;

public class ListStyleButtonField extends Field
{
    public static final int DRAWPOSITION_TOP = 0;
    public static final int DRAWPOSITION_BOTTOM = 1;
    public static final int DRAWPOSITION_MIDDLE = 2;
    public static final int DRAWPOSITION_SINGLE = 3;
    
    private static final int LISTSTYLE_ALARMCLOCK=0;
    private static final int LISTSTYLE_WORLDCLOCK=1;
    private static final int LISTSTYLE_REMINDER=2;
    
    private static final int DEFAULT_CORNER_RADIUS = 12;
    
    private static final int HPADDING = Display.getWidth() <= 320 ? 4 : 6;
    private static final int VPADDING = 4;
    
    private static final boolean USING_LARGE_ICON = Display.getWidth()<640 ? false : true;
    
    private static int COLOR_BACKGROUND = 0xFFFFFF;
    private static int COLOR_BORDER = 0xBBBBBB;
    private static int COLOR_BACKGROUND_FOCUS = 0x186DEF;
    //private static final int COLOR_LIST_DISABLED = Color.DARKGRAY;//0x6D6D6D;//0x6A6A6A;

    //private Bitmap _leftIcon;
    private Bitmap _iconNormal; //=USING_LARGE_ICON ? Bitmap.getBitmapResource("folder_large.png") : Bitmap.getBitmapResource("folder_small.png");

    private MyLabelField _labelTitle;
    private MyLabelField _labelDescription;

    private MyLabelField _labelPlaceItem;
    //private MyLabelField _labelDate;

    private int _rightOffset;
    private int _leftOffset;
    private int _labelHeight;
    //private int _listStyle;
    
    private int _drawPosition = -1;
    private int _corner_radius = DEFAULT_CORNER_RADIUS;
    
	//private static PersistentObject _store;
	//private static Vector _data;
	//private static int _fontSize=1;

	private static Font FONT_TITLE;
	private static Font FONT_DESCRIPTION;
	//private static Font FONT_DATE;
	private static Font FONT_PLACEITEM;

    public ListStyleButtonField(Bitmap bitmap)
    {
        super(USE_ALL_WIDTH | Field.FOCUSABLE);

        _labelTitle = new MyLabelField("title");
        //_labelDescription = new MyLabelField("date");
        //_labelDate = new MyLabelField(folder.getParentPath());
        
        setFontSize();

        _corner_radius = DEFAULT_CORNER_RADIUS;

		//_iconNormal = USING_LARGE_ICON ? Bitmap.getBitmapResource("normal_large.png") : Bitmap.getBitmapResource("normal_small.png");
        _iconNormal = bitmap;
    }

    public ListStyleButtonField(Place placeItem)
    {
        super(USE_ALL_WIDTH | Field.FOCUSABLE);

        StringBuffer showName = new StringBuffer();

        if (!placeItem.getName().equalsIgnoreCase("")) {showName.append(placeItem.getName()).append(", ");}
        if (!placeItem.getAdmin3().equalsIgnoreCase("")) {showName.append(placeItem.getAdmin3()).append(", ");}
        if (!placeItem.getAdmin1().equalsIgnoreCase("")) {showName.append(placeItem.getAdmin1()).append(", ");}
        if (!placeItem.getCountry().equalsIgnoreCase("")) {showName.append(placeItem.getCountry());}

        _labelPlaceItem = new MyLabelField(showName.toString());

        setFontSize();

        _corner_radius = 0;
    }

    public void setDrawPosition(int drawPosition)
    {
        _drawPosition = drawPosition;
    }

    public void layout(int width, int height)
    {
    	if (_iconNormal != null)
    	{
            _leftOffset = _iconNormal.getWidth() + HPADDING * 2;    		
    	} else {
    		_leftOffset = HPADDING * 2;
    	}

        _rightOffset = HPADDING;

        if (_labelTitle != null)
        {
            _labelTitle.layout(width - _leftOffset - _rightOffset, height); // - (_labelDate != null ? _labelDate.getWidth() : 0), height);
            _labelHeight = _labelTitle.getHeight();
        }
        
    	if (_labelDescription != null)
    	{
    		_labelDescription.layout(width - _leftOffset - _rightOffset, height); // - (_labelData != null ? _labelDate.getWidth() : 0, height);
    		_labelHeight += _labelDescription.getHeight();
    	}

    	if (_labelPlaceItem != null)
    	{
    		_labelPlaceItem.layout(width- HPADDING * 2 - _rightOffset, height);
    		_labelHeight = _labelPlaceItem.getHeight();
    	}

        setExtent(width, _labelHeight+10);//+2* extraVPaddingNeeded);
    }
    
    public void setFontSize()
    {
  		FONT_TITLE = WeatherAppConfig.FONT_LIST_TITLE;
    	//FONT_DATE = vDiskAppConfig.FONT_DATE_NORMAL;
    	//FONT_DESCRIPTION = vDiskAppConfig.FONT_LIST_DESCRIPTION;
  		FONT_PLACEITEM = WeatherAppConfig.FONT_PLACE_ITEM;

    	if(_labelTitle!=null) {_labelTitle.setFont(FONT_TITLE);}
    	//if(_labelDate!=null) {_labelDate.setFont(FONT_DATE);}
    	//if(_labelDescription != null) {_labelDescription.setFont(FONT_DESCRIPTION);}
    	if (_labelPlaceItem != null) {_labelPlaceItem.setFont(FONT_PLACEITEM);}
    }

    /*
    static
	{
    	_store=PersistentStore.getPersistentObject(0xaa041103a4a17b66L); //blackberry_alarmclock_worldclock_settings_id
		
		synchronized(_store)
		{
			if(_store.getContents()==null)
			{
				_store.setContents(new Vector());
				_store.forceCommit();
			}
		}
		
		try {
			_data=new Vector();
			_data=(Vector) _store.getContents();
		} catch (final Exception e) {}
	}
	*/

    protected void paint(Graphics g)
    {
        // Logo Bitmap
    	if (_iconNormal != null)
    	{
       		g.drawBitmap(HPADDING, (getHeight()-_iconNormal.getHeight())/2, _iconNormal.getWidth(), _iconNormal.getHeight(), _iconNormal, 0, 0);    		
    	}

        // Title Text
    	if (_labelTitle != null)
    	{
            try
            {
            	g.setFont(FONT_TITLE);
            	g.pushRegion(_leftOffset, VPADDING, _labelTitle.getWidth(), _labelTitle.getHeight(), 0, 0);
                _labelTitle.paint(g);
            } finally {g.popContext();}
    	}
        
        // Right Text
        //if(_labelDate!=null)
        //{
        //	try
        //	{
        //		g.setFont(FONT_DATE);
        //		g.pushRegion(getWidth() - HPADDING - _labelDate.getWidth(), (getHeight() - _labelDate.getHeight())/2, _labelDate.getWidth(), _labelDate.getHeight(),0,0);
        //		_labelDate.paint(g);
        //	} finally{
        //		g.popContext();
        //	}       	
        //}

        // Description Text
        if (_labelDescription != null)
        {
        	try
        	{
        		g.setColor(g.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS) ? Color.WHITE : Color.GRAY);
        		//if (g.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS))
        		//{
        		//	g.setColor(Color.WHITE);
        		//} else {
        		//	g.setColor(Color.GRAY);	
    			//}

            	g.setFont(FONT_DESCRIPTION);
            	g.pushRegion(_leftOffset, getHeight()-_labelDescription.getHeight()-VPADDING, _labelDescription.getWidth(), _labelDescription.getHeight(),0,0);
            	_labelDescription.paint(g);
        	} finally {g.popContext();}	
        }

        // Place Item Text
        if (_labelPlaceItem != null)
        {
        	try {
				g.setFont(FONT_PLACEITEM);
				g.pushRegion(_leftOffset, (getHeight() - _labelPlaceItem.getHeight())/2, _labelPlaceItem.getWidth(), _labelPlaceItem.getHeight(), 0, 0);
				_labelPlaceItem.paint(g);
			} finally {g.popContext();}
        }
    }

    protected void paintBackground(Graphics g)
    {
        if(_drawPosition < 0)
        {
            super.paintBackground(g);
            return;
        }
        
        int oldColour = g.getColor();
        
        switch(0)
        {
			case LISTSTYLE_ALARMCLOCK:
        		COLOR_BACKGROUND_FOCUS = 0x186DEF;
        		
        		break;
			case LISTSTYLE_WORLDCLOCK:
        		COLOR_BACKGROUND_FOCUS = 0x0A9000;
        		
        		break;
			case LISTSTYLE_REMINDER:
        		COLOR_BACKGROUND_FOCUS = 0xC72F00;

        		break;
		}

        int background = g.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS) ? COLOR_BACKGROUND_FOCUS : COLOR_BACKGROUND;

        try {
            if(_drawPosition == 0)
            {
                // Top
                g.setColor(background);
                g.fillRoundRect(0, 0, getWidth(), getHeight() + _corner_radius, _corner_radius, _corner_radius);
                g.setColor(COLOR_BORDER);
                g.drawRoundRect(0, 0, getWidth(), getHeight() + _corner_radius, _corner_radius, _corner_radius);
                g.drawLine(0, getHeight() - 1, getWidth(), getHeight() - 1);
            } else if(_drawPosition == 1)
            {
                // Bottom 
                g.setColor(background);
                g.fillRoundRect(0, -_corner_radius, getWidth(), getHeight() + _corner_radius, _corner_radius, _corner_radius);
                g.setColor(COLOR_BORDER);
                g.drawRoundRect(0, -_corner_radius, getWidth(), getHeight() + _corner_radius, _corner_radius, _corner_radius);
            } else if(_drawPosition == 2)
            {
                // Middle
                g.setColor(background);
                g.fillRoundRect(0, -_corner_radius, getWidth(), getHeight() + 2 * _corner_radius, _corner_radius, _corner_radius);
                g.setColor(COLOR_BORDER);
                g.drawRoundRect(0, -_corner_radius, getWidth(), getHeight() + 2 * _corner_radius, _corner_radius, _corner_radius);
                g.drawLine(0, getHeight() - 1, getWidth(), getHeight() - 1);
            } else {
                // Single
                g.setColor(background);
                g.fillRoundRect(0, 0, getWidth(), getHeight(), _corner_radius, _corner_radius);
                g.setColor(COLOR_BORDER);
                g.drawRoundRect(0, 0, getWidth(), getHeight(), _corner_radius, _corner_radius);
            }
        } finally {
            g.setColor(oldColour);
        }
    }

    protected void drawFocus(Graphics g, boolean on)
    {
        if(_drawPosition < 0)
        {
        } else {
            boolean oldDrawStyleFocus = g.isDrawingStyleSet(Graphics.DRAWSTYLE_FOCUS);
            try {
                if(on)
                {
                	g.setColor(Color.WHITE);
                    g.setDrawingStyle(Graphics.DRAWSTYLE_FOCUS, true);
                }
                paintBackground(g);
                paint(g);
            } finally {
                g.setDrawingStyle(Graphics.DRAWSTYLE_FOCUS, oldDrawStyleFocus);
            }
        }
    }

    protected boolean keyChar(char character, int status, int time) 
    {
    	switch (character)
    	{
			case Characters.ENTER:
	            clickButton();
	            return true;
        }

        return super.keyChar(character, status, time);
    }

    protected boolean navigationUnclick(int status, int time) 
    {
    	if ((status & KeypadListener.STATUS_FOUR_WAY) == KeypadListener.STATUS_FOUR_WAY)
    	{
        	clickButton();
        	return true;
    	}

    	return super.navigationClick(status, time);
    }

    protected boolean trackwheelClick(int status, int time)
    {
    	if ((status & KeypadListener.STATUS_TRACKWHEEL) == KeypadListener.STATUS_TRACKWHEEL)
    	{
       		clickButton();
       		return true;
    	}

    	return super.trackwheelClick(status, time);
    }

    protected boolean invokeAction(int action) 
    {
    	switch(action)
    	{
    		case ACTION_INVOKE:
           		clickButton();
           		return true;
    	}

    	return super.invokeAction(action);
    }

    protected boolean touchEvent(TouchEvent message)
    {
        int x = message.getX(1);
        int y = message.getY(1);

        if (x < 0 || y < 0 || x > getExtent().width || y > getExtent().height) {return false;}

        switch (message.getEvent())
        {
            case TouchEvent.UNCLICK:
           		clickButton();
           		return true;
        }

        return super.touchEvent(message);
    }

    public void clickButton() {fieldChangeNotify(0);}

    public void setDirty(boolean dirty) {}
    public void setMuddy(boolean muddy) {}
}

class MyLabelField extends LabelField
{
    public MyLabelField(String text) {super(text, LabelField.ELLIPSIS);}

	public void layout(int width, int height) {super.layout(width, height);}   

	public void paint(Graphics g) {super.paint(g);}
}